package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PUserBrokerageRecordBo;
import com.ruoyi.system.domain.vo.PUserBrokerageRecordVo;
import com.ruoyi.system.domain.PUserBrokerageRecord;
import com.ruoyi.system.mapper.PUserBrokerageRecordMapper;
import com.ruoyi.system.service.IPUserBrokerageRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户佣金记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-26
 */
@RequiredArgsConstructor
@Service
public class PUserBrokerageRecordServiceImpl implements IPUserBrokerageRecordService {

    private final PUserBrokerageRecordMapper baseMapper;

    /**
     * 查询用户佣金记录
     */
    @Override
    public PUserBrokerageRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户佣金记录列表
     */
    @Override
    public TableDataInfo<PUserBrokerageRecordVo> queryPageList(PUserBrokerageRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PUserBrokerageRecord> lqw = buildQueryWrapper(bo);
        Page<PUserBrokerageRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户佣金记录列表
     */
    @Override
    public List<PUserBrokerageRecordVo> queryList(PUserBrokerageRecordBo bo) {
        LambdaQueryWrapper<PUserBrokerageRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PUserBrokerageRecord> buildQueryWrapper(PUserBrokerageRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PUserBrokerageRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUid() != null, PUserBrokerageRecord::getUid, bo.getUid());
        lqw.eq(bo.getOrderId() != null, PUserBrokerageRecord::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), PUserBrokerageRecord::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), PUserBrokerageRecord::getTitle, bo.getTitle());
        lqw.eq(bo.getPrice() != null, PUserBrokerageRecord::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getMark()), PUserBrokerageRecord::getMark, bo.getMark());
        lqw.eq(bo.getBrokerageLevel() != null, PUserBrokerageRecord::getBrokerageLevel, bo.getBrokerageLevel());
        return lqw;
    }

    /**
     * 新增用户佣金记录
     */
    @Override
    public Boolean insertByBo(PUserBrokerageRecordBo bo) {
        PUserBrokerageRecord add = BeanUtil.toBean(bo, PUserBrokerageRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户佣金记录
     */
    @Override
    public Boolean updateByBo(PUserBrokerageRecordBo bo) {
        PUserBrokerageRecord update = BeanUtil.toBean(bo, PUserBrokerageRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PUserBrokerageRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户佣金记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void insertFront(PUserBrokerageRecord record) {
        baseMapper.insert(record);
    }

    @Override
    public TableDataInfo<PUserBrokerageRecord> queryMyLogList(PUserBrokerageRecord bo, PageQuery pageQuery) {
        QueryWrapper<PUserBrokerageRecord> lqw = Wrappers.query();
        lqw.eq(ObjectUtil.isNotEmpty(bo.getUid()), "r.uid", bo.getUid());
        lqw.groupBy("r.id");
        Page<PUserBrokerageRecord> result = baseMapper.selectLeftPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
}
